//
//  LoadedWeiBoViewController.m
//  uniquelove
//
//  Created by ibokan on 14-12-6.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "LoadedWeiBoViewController.h"
#import "AFNetworking.h"
#import "WeiboSDK.h"
@interface LoadedWeiBoViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
- (IBAction)changeAccount:(id)sender;

@end

@implementation LoadedWeiBoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *token = (NSString *)[[NSUserDefaults standardUserDefaults]objectForKey:@"accessToken"];
	NSDictionary *dic = @{@"access_token": token};
    //获取微博内容
    [manager GET:@"https://api.weibo.com/2/statuses/user_timeline.json" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSLog(@"%@",responseObject);
        //获取用户昵称
        self.nameLabel.text = dic[@"statuses"][0][@"user"][@"screen_name"];
        
        
        [[NSUserDefaults standardUserDefaults]setObject:self.nameLabel.text forKey:@"userName"];
      
        //获取用户头像
        NSURL *imageURL = [NSURL URLWithString:dic[@"statuses"][0][@"user"][@"profile_image_url"]];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        self.imageView.image = [UIImage imageWithData:data];
        [[NSUserDefaults standardUserDefaults]setObject:data forKey:@"imageData"];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}


- (IBAction)changeAccount:(id)sender {
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kRedirectURL;
    request.scope = @"all";
    [WeiboSDK sendRequest:request];
}
@end
